package com.gkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkd.constant.DefaultPassword;
import com.gkd.constant.MessageConstant;
import com.gkd.constant.RoleConstant;
import com.gkd.exception.AccountLockedException;
import com.gkd.exception.AccountNotFoundException;
import com.gkd.exception.PasswordErrorException;
import com.gkd.pojo.dto.UserDTO;
import com.gkd.pojo.dto.UserLoginDTO;
import com.gkd.pojo.dto.UserPageQueryDTO;
import com.gkd.pojo.entity.User;
import com.gkd.pojo.vo.UserVO;
import com.gkd.result.PageResult;
import com.gkd.service.UserService;
import com.gkd.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
* @author Administrator
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-06-18 17:09:20
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param userDTO
     */
    @Override
    public void add(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        //密码md5加密
        if (user.getPassword()==null){
            user.setPassword(DefaultPassword.password);
        }
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        userMapper.insert(user);
    }

    /**
     * 用户分页查询
     * @param userPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {
        Page<User> page = new Page(userPageQueryDTO.getCurrent(), userPageQueryDTO.getPageSize());

        //设置查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        //用户名
        wrapper.like(StringUtils.isNotBlank(userPageQueryDTO.getUsername()),User::getUsername,userPageQueryDTO.getUsername());
        //邮箱
        wrapper.like(StringUtils.isNotBlank(userPageQueryDTO.getEmail()),User::getEmail,userPageQueryDTO.getEmail());
        //手机号
        wrapper.like(StringUtils.isNotBlank(userPageQueryDTO.getPhone()),User::getPhone,userPageQueryDTO.getPhone());
        //性别
        wrapper.eq(userPageQueryDTO.getGender()!=null,User::getGender,userPageQueryDTO.getGender());
        //角色
        wrapper.eq(User::getRole,userPageQueryDTO.getRole());

        IPage<User> userPage = userMapper.selectPage(page, wrapper);

        IPage<UserVO> userVOIPage=userPage.convert(user -> {
            return UserVO.builder()
                    .userId(user.getUserId())
                    .username(user.getUsername())
                    .name(user.getName())
                    .email(user.getEmail())
                    .phone(user.getPhone())
                    .gender(user.getGender())
                    .status(user.getStatus())
                    .build();
        });
        return new PageResult(userVOIPage.getTotal(),userVOIPage.getRecords());
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        //查找用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,userLoginDTO.getUsername());
        User user = userMapper.selectOne(wrapper);

        //用户未注册
        if (user==null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        String md5Password =DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        if (!md5Password.equals(user.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //检查是否被禁用
        if (user.getStatus()==0){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return user;
    }
}




